package com.lagou.lagouserviceuser.controller;

import com.lagou.lagouserviceuser.pojo.RegistUser;
import com.lagou.lagouserviceuser.pojo.User;
import com.lagou.lagouserviceuser.service.CodeService;
import com.lagou.lagouserviceuser.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.net.http.HttpResponse;
import java.util.UUID;

/**
 * @Authther: mrwu
 * @date:2021/4/14-04
 * @Description:com.lagou.lagouserviceuser.controller
 */
@Slf4j
@RestController
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    CodeService codeService;
    /**
     * 用户注册接口
     */
    @RequestMapping("/user/register")
    public int register(RegistUser registUser, HttpServletResponse response){
        //验证数据
        //检验是否注册
        //调用短信验证码服务 验证验证码是否正确
        User user1 = userService.getuserByEmail(registUser.getEmail());
        if (user1!=null){
            log.info("已经注册");
            return 1;
        }
        boolean isvalidate = codeService.isvalidate(registUser.getCode(), registUser.getEmail());
          if (isvalidate){
              User user = new User();
              user.setEmail(registUser.getEmail());
              user.setPassword(registUser.getPassword());
              boolean registsucess = userService.registUser(user);
              if (registsucess){
               createtoken(registUser.getEmail(), response);
              }
              return   0;
          }
          log.info("验证码不正确");
          return 2;//注册失败
    }
    /**
     * 用户登陆接口
     */
    @GetMapping("/user/login/{email}/{password}")
    public boolean login(@PathVariable("email")String eamil,@PathVariable("password") String password,HttpServletResponse response){
        User user = new User();
        user.setEmail(eamil);
        user.setPassword(password);
        User user1 = userService.quryUser(user);
        if (user1==null){
          return false;
        }
        createtoken(user.getEmail(), response);
        return true;
    }

    private void  createtoken(String email,HttpServletResponse response){
        //刷新token
        UUID uuid = UUID.randomUUID();
        //写入缓存
        redisTemplate.opsForValue().set(uuid.toString(), email);

        response.addHeader("token", uuid.toString());
        response.setHeader("Access-Control-Expose-Headers","token");
    }

    /**
     * 获取用户邮箱接口
     */
    @GetMapping("/user/info/{token}")
    public String getEmail(@PathVariable("token") String token){

        return (String) redisTemplate.opsForValue().get(token);
    }

    @RequestMapping("/user/token")
    public void testcookie(HttpServletResponse response){
        Cookie token = new Cookie("mytoken", "test-cookie");
        token.setPath("/");
        response.addCookie(token);
    }
}
